package com.sjm.suchbear.minibookmall;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.Utils.T;
import com.sjm.suchbear.minibookmall.action.UserManage;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends Activity {

    @BindView(R.id.ll_header_back)
    LinearLayout llHeaderBack;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.iv_header_function_ico)
    ImageView ivHeaderFunctionIco;
    @BindView(R.id.ll_header_function)
    LinearLayout llHeaderFunction;
    @BindView(R.id.ll_List_Main)
    RelativeLayout llListMain;
    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_repassword)
    EditText etRepassword;
    @BindView(R.id.et_realname)
    EditText etRealname;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.cb_manager)
    CheckBox cbManager;

    private SQLiteDatabase dbread;

    private String name;
    private String password;
    private String repassword;
    private String realname;
    private String phone;
    private String address;

    private static int LOGIN_RESULT = 0x222;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        BookDB db = new BookDB(getBaseContext());
        dbread = db.getReadableDatabase();
    }

    @OnClick({R.id.ll_header_back, R.id.tv_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_header_back:
                finish();
                break;
            case R.id.tv_register:
                register();
                break;
        }
    }

    private void register() {
        name = String.valueOf(etUsername.getText());
        password = String.valueOf(etPassword.getText());
        repassword = String.valueOf(etRepassword.getText());
        realname = String.valueOf(etRealname.getText());
        phone = String.valueOf(etPhone.getText());
        address = String.valueOf(etAddress.getText());
        if (!UserManage.vaildUsername(dbread, name)) {
            T.showShort(getBaseContext(), "用户名已存在");
            etUsername.requestFocus();
            return;
        }
        if (!password.equals(repassword)) {
            T.showShort(getBaseContext(), "两次密码输入不同");
            return;
        }
        if (cbManager.isChecked()) {
            UserManage.addUser(dbread, name, password, realname, phone, address, 1, "1000.00");
            T.showShort(getBaseContext(), "注册成功");
            Intent backIntent = new Intent();
            backIntent.putExtra("loginUsername", name);
            setResult(LOGIN_RESULT, backIntent);
            finish();
        } else {
            if (realname.trim().equals("")) {
                T.showShort(getBaseContext(), "真实姓名不得为空");
                etRealname.requestFocus();
                return;
            }
            if (phone.trim().equals("")) {
                T.showShort(getBaseContext(), "手机号不得为空");
                etPhone.requestFocus();
                return;
            }
            if (address.trim().equals("")) {
                T.showShort(getBaseContext(), "收货地址不得为空");
                etAddress.requestFocus();
                return;
            }
            UserManage.addUser(dbread, name, password, realname, phone, address, 0, "1000.00");
            T.showShort(getBaseContext(), "注册成功");
            Intent backIntent = new Intent();
            backIntent.putExtra("loginUsername", name);
            setResult(LOGIN_RESULT, backIntent);
            finish();
        }
    }
}
